package com.zf.yichat.api.controller.message;

import com.zf.yichat.api.controller.BaseController;
import com.zf.yichat.api.dto.request.MessageCollectionRequest;
import com.zf.yichat.dto.MessageCollectionDto;
import com.zf.yichat.model.MessageCollection;
import com.zf.yichat.service.MessageCollectionService;
import com.zf.yichat.utils.common.N3d;
import com.zf.yichat.utils.response.FsResponse;
import com.zf.yichat.utils.response.FsResponseGen;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 信息收藏接口
 * @author wu
 */
@RestController
@RequestMapping("messageCollection")
@Slf4j
public class MessageCollectionController extends BaseController {

    @Autowired
    private MessageCollectionService collectionService;

    /**
     * 收藏列表
     * @return
     */
    @PostMapping("list")
    public FsResponse list() {
        List<MessageCollectionDto> list = collectionService.queryCollectionListByUserId(getUserId());
        return FsResponseGen.successData(list);
    }


    /**
     * 新增收藏列表
     * @param params
     * @return
     */
    @PostMapping("addCollection")
    public FsResponse addCollection(@RequestBody MessageCollectionRequest params) throws Exception {

        String friendUserId = params.getFriendUserId();
        if (StringUtils.isBlank(friendUserId)) {
            return FsResponseGen.failMsg("参数错误[userId为空]");
        }

        // 解密UserId
        Long decryptFriendUserId = N3d.decode(friendUserId);

        MessageCollection collection = new MessageCollection();
        BeanUtils.copyProperties(params, collection);
        collection.setFriendUserId(decryptFriendUserId);
        collection.setUserId(getUserId());

        return FsResponseGen.successData(collectionService.addCollection(collection));
    }

    /**
     * 删除收藏列表
     * @param params
     * @return
     */
    @PostMapping("deleteCollection")
    public FsResponse deleteCollection(@RequestBody MessageCollectionRequest params) {
        Integer id = params.getId();
        return FsResponseGen.successData(collectionService.deleteCollectionById(id));
    }
}
